import pymysql


class Dbtools():
    @classmethod
    def __create_com(cls):
        conn = pymysql.connect(host="localhost", port=3306, user="root",
                               password="123456", database="cje", charset="utf8")
        return  conn
    def query_one(self,sql):
        conn = None
        my_cursor = None
        try:
            conn = Dbtools.__create_com()
            my_cursor = conn.cursor()
            my_cursor.execute(sql)
            res = my_cursor.fetchall()
        except Exception as e:
            print("错误原因是：",str(e))
        finally:
            my_cursor.cloes()
            conn.close()
            return res


    def db_uid(self,sql):
        conn = None
        my_cursor = None
        try:
            conn = Dbtools.__create_com()
            my_cursor = conn.cursor()
            my_cursor.execute(sql)
            conn.commit()
            print("Affected rows:",conn.affected_rows())
        except Exception as e:
            print("错误原因是：", str(e))
            conn.rollback()
        finally:
            my_cursor.cloes()
            conn.close()


# if __name__ == '__main__':
#     print(Dbtools().query_one("select version();"))
